package com.pateo.springcloudorder.web.controller;

import com.pateo.springcloudorder.dao.model.auto.AutoOrder;
import com.pateo.springcloudorder.service.OrderService;
import com.pateo.springcloudorder.web.request.OrderRequest;
import com.pateo.springcloudorder.web.response.OrderResponse;
import com.pateo.springcloudorder.web.response.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@Api(tags = "订单管理接口")
@RestController
public class OrderController {

    @Autowired
    private OrderService goodsService;

    @ApiOperation(value = "创建订单", httpMethod = "POST", response = OrderResponse.class, notes = "新增订单信息")
    @RequestMapping(value = "/addOrder", method = RequestMethod.POST)
    public Response addOrder(@Valid @RequestBody OrderRequest request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error(bindingResult.getAllErrors().toString());
            return new Response("0001", bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        AutoOrder autoOrder = new AutoOrder();
        BeanUtils.copyProperties(request, autoOrder);
        goodsService.insertOrder(autoOrder);
        return new Response();
    }

    @ApiOperation(value = "支付订单", response = OrderResponse.class, notes = "支付订单")
    @RequestMapping(value = "/payOrder/{orderNumber}", method = RequestMethod.POST)
    public Response payOrder(@PathVariable String orderNumber) {
        AutoOrder order = goodsService.payOrder(orderNumber);
        return new Response(order);
    }

}
